import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== Declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page				= $Lazy.load(()=>import('#tp/page/page-container'))
const TableComponent 	= $Lazy.state(()=>import('./cpt/table-component'))
const $cache 			= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		forms : [
			{ label: '类型名称',		name: 'temp_name' },
			{ label: '类型代码',		name: 'temp_code' },
		],
		// 提交表单列表
		formData : []
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	// 获取下拉数据
	getModalSelect = (callback, isEdit) => {
		let { formData } = this.state
		
		$cache.then( async f => {
			// 危急值、参考区间、弱阳性、阳性、阴性
			if(!$fn.hasArray(formData)){
				this.dic = await f.getDicCache({ code: [ 70100, 70000 ]})
				const { dic } = this
				const forms = dic['70000'].map(v=>({ label:v.name, name:v.code, half:true, type:'select', loading:true, idStr:'code' }))
				formData = [
					{ label: '类型名称',		name: 'temp_name', 		full:true, required:true,  },
					...forms,
					{ label: '排序',			name: 'sort', 			half:true, type:'number' },
					{ label: '说明',			name: 'remark', 		full:true, type:'textarea' },
				]
				
				$fn.setModalRows(formData, '70001', ()=>{
					$cache.then( async f => {
						// 危急值下限
						$fn.setRow(formData, '70007', dic['70100'])
						// 危急值上限
						$fn.setRow(formData, '70006', dic['70100'])
						// 参考区间下限
						$fn.setRow(formData, '70005', dic['70100'])
						// 参考区间上限
						$fn.setRow(formData, '70004', dic['70100'])
						// 弱阳性
						$fn.setRow(formData, '70002', dic['70100'])
						// 阴性
						$fn.setRow(formData, '70003', dic['70100'])
						// 阳性
						$fn.setRow(formData, '70001', dic['70100'])
						this.setState({ formData }, ()=>{
							callback?.()
						})
					})
				})
			}else{
				if(!isEdit){ callback?.() }
			}
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'result-tips-temp/index')
	
	// 表单
	cols = [
		{ title: '类型名称',		dataIndex: 'temp_name', 	width:150 },
		{ title: '类型代码', 	dataIndex: 'temp_code', 	width:150 },
		{ title: '排序', 		dataIndex: 'sort', 			...$fn.table.num },
		{ title: '说明', 		dataIndex: 'remark', 		width:150 },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'result-tips-temp/info', { uuid },{
				select: ()=>{
					this.getModalSelect(()=>{
						if(!this.modal){
							setTimeout(()=> this.modal?.open({formRef:this.formRef}), 200 )
						}
					}, true)
				},
				echo: data => {
					const { content } = data
					if($fn.hasArray(content)){ content.forEach(v=> data[v.type_code] = v.tips_code ) }
					return data
				}
			}),				// 编辑
			this.onDel([ uuid ]),																// 删除
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.click.call(this, '添加', ()=>{
				this.getModalSelect(()=>{
					setTimeout(()=> this.modal?.open({formRef:this.formRef}), 200 )
				})
			}),			
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'result-tips-temp/del', { uuid, isKeys: !uuid })
	render(){
		return (
			<>
				<Page title='结果提示' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{ onRow: (rows) => {this.tableComRef.fetch(rows.uuid)}, split:true }) }
					{/* 表格 */}
					<TableComponent onRef={ ref => this.tableComRef = ref }/>
				</Page>
				{/* 添加、编辑 */}
				{
					$fn.hasArray(this.state.formData) && AddEditModal.call(this,{ 
						api: 'result-tips-temp/add', 
						editApi:'result-tips-temp/edit',
						labelWidth:'s8',
						width:700,
						getSubmit: param => {
							let stack = []
							for(let i in param ){
								if(i.indexOf('700') >= 0){
									stack.push({ type_code:i, tips_code: param[i] || '' })
								}
							}
							
							for(let i in param ){
								if(i.indexOf('700') >= 0){
									delete param[i]
								}
							}
							
							return { content: stack }
						}
					})
				}
			</>
		)
	}
}

export default Index